// by Xeno

private ["_display", "_groupcolor", "_othercolor", "_units", "_index", "_control", "_targetPos", "_position", "_col"];

if (!local player) exitwith {};

#define IDCPLAYER 5200
if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3",diag_tickTime,"playernamehud.sqf","Starting Script!"]; };

disableSerialization;

x_pm_received_ar = [];
x_pm_send_ar = [];
x_pm_add_ar = [];
x_player_name = (if (alive player ) then { name player; } else { ""; });
x_pm_send_ar_update = false;

5 cutrsc["PlayerNameHud","PLAIN"];

//_groupcolor = [0.7,0.7,0,1];
_groupcolor = [0.2,0.7,0.2,1];
_othercolor = [0.3,0.3,1,1];

if (isNil "x_show_pname_hud") then {
	If (!isnil "ShowPlayerNames") then {
		x_show_pname_hud = ShowPlayerNames;
	} else {
		x_show_pname_hud = if (cadetmode) then {false} else {true};
	};
	d_show_player_namesx = if (x_show_pname_hud) then {1} else {0};
};
if (isNil "d_show_player_namesx") then {d_show_player_namesx = 0};
if (isNil "d_blockspacebarscanning") then {d_blockspacebarscanning = False};
if (d_BlockSpacebarScanning) then {
	X_KeyboardHandlerKeyDown = {if ((_this select 1) == 57) then {true} else {false}};
	waitUntil {!isNull (findDisplay 46)};
	(findDisplay 46) displayAddEventHandler ["KeyDown", "_this call X_KeyboardHandlerKeyDown"];
};

sleep 10;

waitUntil {!isNil "d_player_entities"};
if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3",diag_tickTime,"playernamehud.sqf","Has players entities"]; };
_units = d_player_entities;
waitUntil {!isNil {g_oVariableStorage getVariable (d_player_entities select ((count d_player_entities) - 1))}};

if (d_global_debug > 0) then { diag_log Format["(%1)[%2]: %3",diag_tickTime,"playernamehud.sqf","Starting the loop"]; };
if (ShowPlayerIndicatorDistance < ShowPlayerNamesDistance) then {
	ShowPlayerIndicatorDistance = ShowPlayerNamesDistance;
};
while {true} do {
	if (x_show_pname_hud) then {
		{
			_u = missionNamespace getVariable _x;
			if (!isNil "_u") then {
				if (!isNull _u && _u != player && alive player && alive _u && player distance _u <= ShowPlayerIndicatorDistance) then {
					_ua = g_oVariableStorage getVariable _x;
					_index = _ua select 0;
					_control = uiNameSpace getVariable "X_PHUD" displayCtrl (IDCPLAYER + _index);
					if (isNil "_control") exitWith {};
					if (!surfaceiswater (position _u)) then {
						_targetPos = getPosATL _u;
						_targetPos set [2 , (_targetPos select 2) + 2];
					} else {
						_targetPos = getPosASL _u;
						_targetPos set [2 , (_targetPos select 2) + 1.3];
					};

					_position = worldToScreen _targetPos;

					if (((count _position) != 0) AND (!visibleMap)) then {
						_control ctrlShow true;
						_control ctrlSetPosition [(_position select 0)-0.01,(_position select 1)-0.01];
						private "_tex";
						_tex = switch (d_show_player_namesx) do {
							case 1: {name _u};
							case 2: {_ua select 1};
							case 3: {str(9 - round(9 * damage _u))};
							default {"Error"};
						};
						//diag_log Format["playernamehud: %1", _u];
						if (isNil "_tex") then {_tex = "Error"};
						If ((player distance _u) > ShowPlayerNamesDistance) then { _tex = "'" };
						_control ctrlSetText _tex;
						_col = if (group _u == group player) then {_groupcolor} else {_othercolor};
						_control ctrlSetTextColor _col;
					} else {
						_control ctrlShow false;
					};
					_control ctrlCommit 0;
					waituntil {ctrlCommitted _control};
				} else {
					_ua = g_oVariableStorage getVariable _x;
					if (!isNil "_ua") then {
						_index = _ua select 0;
						_control = uiNameSpace getVariable "X_PHUD" displayCtrl (IDCPLAYER + _index);
						if (isNil "_control") exitWith {};
						_control ctrlShow false;
						_control ctrlCommit 0;
						waituntil {ctrlCommitted _control};
					};
				};
			} else {
				_ua = g_oVariableStorage getVariable _x;
				if (!isNil "_ua") then {
					_index = _ua select 0;
					_control = uiNameSpace getVariable "X_PHUD" displayCtrl (IDCPLAYER + _index);
					if (isNil "_control") exitWith {};
					_control ctrlShow false;
					_control ctrlCommit 0;
					waituntil {ctrlCommitted _control};
				};
			};
		} foreach _units;
	};
	sleep 0.01;
	
	if (!x_show_pname_hud) then {
		{
			_ua = g_oVariableStorage getVariable _x;
			if (!isNil "_ua") then {
				_index = _ua select 0;
				_control = uiNameSpace getVariable "X_PHUD" displayCtrl (IDCPLAYER + _index);
				_control ctrlShow false;
				_control ctrlCommit 0;
				waituntil {ctrlCommitted _control};
			};
		} foreach _units;
		waituntil {sleep 0.23;x_show_pname_hud};
	};
};